#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_INIT
AC_CONFIG_SRCDIR([src/core/main.c])
AM_INIT_AUTOMAKE(fcitx, "4.0.1")

dnl Find out what type of system we are
AC_CANONICAL_HOST
AC_CONFIG_HEADERS(config.h)

m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])])

# Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_PROG_RANLIB
AC_PROG_LIBTOOL

# Checks for libraries.
AM_ICONV

# Checks for header files.
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS([limits.h malloc.h malloc/malloc.h stdlib.h string.h sys/param.h unistd.h wait.h sys/wait.h machine/endian.h pthread.h])

AC_MSG_CHECKING([host platform characteristics])

case "$host" in
    *-*-*kfreebsd*)
    HOST_LIBS="-ldl -lpthread"
    md5prog=md5sum
    ;;
    *-*-*freebsd*)
    HOST_LIBS="-lexecinfo -pthread"
    md5prog=gmd5sum
    ;;
    *-*-linux*)
    HOST_LIBS="-ldl -lpthread"
    md5prog=md5sum
    ;;
    *-*-gnu*)
    HOST_LIBS="-ldl -lpthread"
    md5prog=md5sum
    ;;
esac

AC_SUBST(HOST_LIBS)

AC_PATH_PROGS(WGET, wget, false)
if test "x$WGET" = "xfalse"
then
    AC_MSG_WARN([without wget you cannot download data automatically])
fi

AC_PATH_PROGS(TAR, tar, no)
if test "x$TAR" = "xno";
then
    AC_MSG_ERROR([You need to install tar])
fi

AC_PATH_PROGS(MD5SUM, $md5prog, no)
if test "x$MD5SUM" = "xno";
then
    AC_MSG_ERROR([You need to install md5sum])
fi
AC_SUBST(WGET)
AC_SUBST(TAR)
AC_SUBST(MD5SUM)

# x11
AC_PATH_X
test -n "$x_libraries" && X_LIBS="-L$x_libraries"
test -n "$x_includes" && X_CFLAGS="-I$x_includes"
AC_SUBST(X_LIBS)
AC_SUBST(X_CFLAGS)

PKG_CHECK_MODULES(XRENDER, xrender, have_xrender=true, :)
if test "x$have_xrender" = "xtrue"; then
    AC_SUBST(XRENDER_LIBS)
    AC_SUBST(XRENDER_CFLAGS)
    :
else
    AC_MSG_ERROR([No XRender Lib found!])
    :
fi

PKG_CHECK_MODULES(CAIRO_XLIB, cairo-xlib, have_cairo_xlib=true, :)
if test "x$have_cairo_xlib" = "xtrue"; then
    AC_SUBST(CAIRO_XLIB_LIBS)
    AC_SUBST(CAIRO_XLIB_CFLAGS)
    :
else
    AC_MSG_ERROR([No cairo-xlib found!])
    :
fi

# Option to enable the DBus interface for kimpanel support.
AC_ARG_ENABLE(pango,
              AS_HELP_STRING([--enable-pango],
                             [Enable Pango For Better font support]),
              [case "${enableval}" in
               yes)  enable_pango=yes ;;
               no)   enable_pango=no ;;
               auto) enable_pango=auto ;;
               *) AC_MSG_ERROR([bad value "${enableval}" for --enable-dbus, use "yes" or "no" or "auto"(default).]) ;;
               esac],
              [enable_pango=auto]
)

if test "x$enable_pango" = "xno"; then
  # --enable-pango=no
  :
else
    PKG_CHECK_MODULES(PANGOCAIRO, pangocairo, have_pangocairo=true, :)
    if test "x$have_pangocairo" = "xtrue"; then
        AC_SUBST(PANGOCAIRO_LIBS)
        AC_SUBST(PANGOCAIRO_CFLAGS)
        :
    else
        if test "x$enable_pango" = "xyes"; then
            # Must use pango, raise a error.
            AC_MSG_ERROR([No PANGOCAIRO found, configure for pango failed!])
        else
            AC_MSG_WARN([No PANGOCAIRO found, skipping configure for pango])
        fi
    fi
fi
AM_CONDITIONAL(HAVE_PANGOCAIRO, [ test "x$have_pangocairo" = "xtrue"])

if test "x$have_pangocairo" = "xtrue"; then
  AC_DEFINE(_ENABLE_PANGO,,[Enable Pango])
fi

PKG_CHECK_MODULES(FONTCONFIG, fontconfig, have_fontconfig=true, :)
if test "x$have_fontconfig" = "xtrue"; then
    AC_SUBST(FONTCONFIG_LIBS)
    AC_SUBST(FONTCONFIG_CFLAGS)
    :
else
    AC_MSG_ERROR([No fontconfig lib found!])
    :
fi

# Option to enable the DEBUG
AC_ARG_ENABLE(debug,
              AS_HELP_STRING([--enable-debug],[Enable DEBUG]),
              [case "${enableval}" in
               yes)  enable_debug=yes ;;
               no)   enable_debug=no ;;
               *) AC_MSG_ERROR([bad value "${enableval}" for --enable-debug, use "yes" or "no" (default).]) ;;
               esac],
              [enable_debug=no]
)

if test "x$enable_debug" = "xyes"; then
  AC_DEFINE(_DEBUG,,[Enable Debug])
  CFLAGS="$CFLAGS -g"
fi
AM_CONDITIONAL([ENABLE_DEBUG], [test "x$enable_debug" = "xyes"])

# Option to enable the TRAY
AC_ARG_ENABLE(tray,
              AS_HELP_STRING([--disable-tray],[Disable tray icon]),
              [case "${enableval}" in
               yes)  enable_tray=yes ;;
               no)   enable_tray=no ;;
               *) AC_MSG_ERROR([bad value "${enableval}" for --enable-tray, use "yes" (default) or "no".]) ;;
               esac],
              [enable_tray=yes]
)

if test "x$enable_tray" != "xno"; then
  AC_DEFINE(_ENABLE_TRAY,,[Enable Tray Icon])
fi
AM_CONDITIONAL([ENABLE_TRAY], [test "x$enable_tray" != "xno"])

# Option to enable the recording
AC_ARG_ENABLE(recording,
              AS_HELP_STRING([--enable-recording],[Enable recording of user input]),
              [case "${enableval}" in
               yes)  enable_recording=yes ;;
               no)   enable_recording=no ;;
               *) AC_MSG_ERROR([bad value "${enableval}" for --enable-recording, use "yes" or "no" (default).]) ;;
               esac],
              [enable_recording=no]
)

if test "x$enable_recording" = "xyes"; then
  AC_DEFINE(_ENABLE_RECORDING,,[Enable Recording])
fi
AM_CONDITIONAL([ENABLE_RECORDING], [test "x$enable_recording" = "xyes"])

# Option to enable the DBus interface for kimpanel support.
AC_ARG_ENABLE(dbus,
              AS_HELP_STRING([--enable-dbus],[Enable Dbus for kimpanel support]),
              [case "${enableval}" in
               yes)  enable_dbus=yes ;;
               no)   enable_dbus=no ;;
               auto) enable_dbus=auto ;;
               *) AC_MSG_ERROR([bad value "${enableval}" for --enable-dbus, use "yes" or "no" or "auto"(default).]) ;;
               esac],
              [enable_dbus=auto]
)

if test "x$enable_dbus" = "xno"; then
  # --enable-dbus=no
  :
else
  # Checks for Dbus
  PKG_CHECK_MODULES(DBUS, dbus-1 >= 0.2, have_dbus=true, :)

  if test "x$have_dbus" = "xtrue"; then
    AC_SUBST(DBUS_LIBS)
    AC_SUBST(DBUS_CFLAGS)
    :
  else
    if test "x$enable_dbus" = "xyes"; then
      # Must use dbus, raise a error.
      AC_MSG_ERROR([No dbus found, configure for Dbus failed!])
    else
      AC_MSG_WARN([No dbus found, skipping configure for Dbus])
    fi
  fi
fi
AM_CONDITIONAL(HAVE_DBUS, [test "x$have_dbus" = "xtrue"])

if test "x$have_dbus" = "xtrue"; then
  AC_DEFINE(_ENABLE_DBUS,,[Enable DBus])
fi

AC_ARG_ENABLE(table_data,
              AS_HELP_STRING([--disable-table-data],[Disable Table Data Install]),
              [case "${enableval}" in
               yes)  enable_table_data=yes ;;
               no)   enable_table_data=no ;;
               *) AC_MSG_ERROR([bad value "${enableval}" for --enable-table-data, use "yes"(defaule) or "no".]) ;;
               esac],
              [enable_table_data=yes]
)
AM_CONDITIONAL(ENABLE_TABLE_DATA, [test "x$enable_table_data" = "xyes"])


CFLAGS="$CFLAGS -Wall"

# Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_PID_T
AC_TYPE_SIZE_T

# Checks for library functions.
AC_FUNC_FORK
AC_FUNC_MALLOC
AC_TYPE_SIGNAL
AC_CHECK_FUNCS([memmove memset mkdir setlocale strcasecmp strstr strtol asprintf vasprintf])

ALL_LINGUAS="zh_CN"
GETTEXT_PACKAGE=fcitx
AC_SUBST(GETTEXT_PACKAGE)
AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE,"$GETTEXT_PACKAGE", [Gettext package])
AC_PROG_INTLTOOL([0.35.0])
AM_GNU_GETTEXT([external])

AC_SUBST(INCINTL)

#dirty workaround for old intltool autoconf and automake
if test "x$DATADIRNAME" = "x"; then
    DATADIRNAME=share
    AC_SUBST(DATADIRNAME)
fi

AC_CONFIG_FILES([
           Makefile
           fcitx.spec
           doc/Makefile
           doc/man/Makefile
           data/Makefile
           data/table/Makefile
           skin/Makefile
           skin/default/Makefile
           skin/dark/Makefile
           skin/classic/Makefile
           png/Makefile
           lib/Makefile
           src/Makefile
           src/core/Makefile
           src/im/Makefile
           src/im/pinyin/Makefile
           src/im/qw/Makefile
           src/im/special/Makefile
           src/im/extra/Makefile
           src/im/table/Makefile
           src/interface/Makefile
           src/ui/Makefile
           src/tools/Makefile
           src/fcitx-config/Makefile
           src/fcitx-config/fcitx-config.pc
           src/core/fcitx.pc
           tools/Makefile
           test/Makefile
           po/Makefile.in
])
AC_OUTPUT

AC_MSG_RESULT([
Build options:
  Version                       $VERSION
  Install prefix                $prefix
  Build shared libs             $enable_shared
  Build static libs             $enable_static
  Build with tray support       $enable_tray
  Build with pango support      $enable_pango
  Build with dbus support       $enable_dbus
  Build with recording support  $enable_recording
  Build with debug support      $enable_debug
  Install default table data    $enable_table_data
])
